/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.AppAnimal;
import org.springblade.modules.app.service.IAppAnimalService;
import org.springblade.modules.app.vo.AppAnimalVO;
import org.springblade.modules.app.wrapper.AppAnimalWrapper;
import org.springblade.modules.system.service.IDictService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 动物库 控制器
 *
 * @author Blade
 * @since 2019-04-03
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app/appanimal")
@Api(value = "动物库", tags = "动物库接口")
public class AppAnimalController extends BladeController {

	private IAppAnimalService appAnimalService;

	private IDictService dictService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入appAnimal", position = 1)
	public R<AppAnimalVO> detail(AppAnimal appAnimal) {
		AppAnimal detail = appAnimalService.getOne(Condition.getQueryWrapper(appAnimal));
		AppAnimalWrapper appAnimalWrapper = new AppAnimalWrapper(dictService);
		return R.data(appAnimalWrapper.entityVO(detail));
	}

	/**
	 * 分页 动物库
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页", notes = "传入appAnimal", position = 2)
	public R<IPage<AppAnimalVO>> list(AppAnimal appAnimal, Query query) {
		IPage<AppAnimal> pages = appAnimalService.page(Condition.getPage(query), Condition.getQueryWrapper(appAnimal));
		AppAnimalWrapper appAnimalWrapper = new AppAnimalWrapper(dictService);
		return R.data(appAnimalWrapper.pageVO(pages));
	}

	/**
	 * 自定义分页 动物库
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入appAnimal", position = 3)
	public R<IPage<AppAnimalVO>> page(AppAnimalVO appAnimal, Query query) {
		IPage<AppAnimalVO> pages = appAnimalService.selectAppAnimalPage(Condition.getPage(query), appAnimal);
		return R.data(pages);
	}

	/**
	 * 新增 动物库
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入appAnimal", position = 4)
	public R save(@Valid @RequestBody AppAnimal appAnimal) {
		return R.status(appAnimalService.save(appAnimal));
	}

	/**
	 * 修改 动物库
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入appAnimal", position = 5)
	public R update(@Valid @RequestBody AppAnimal appAnimal) {
		return R.status(appAnimalService.updateById(appAnimal));
	}

	/**
	 * 新增或修改 动物库
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入appAnimal", position = 6)
	public R submit(@Valid @RequestBody AppAnimal appAnimal) {
		if (appAnimal.getId() == null) {
			appAnimal.setCreateDate(LocalDateTime.now());
		}
		return R.status(appAnimalService.saveOrUpdate(appAnimal));
	}


	/**
	 * 删除 动物库
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids", position = 7)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(appAnimalService.removeByIds(Func.toIntList(ids)));
	}


}
